<template>
  <j-modal title="就业安置" :width="600" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item label="姓名" v-if="isDisabled" required prop="name">
        <a-input v-model="form.name" :disabled="isDisabled" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="姓名" required prop="realName" v-else>
        <a-select style="width:100%;" v-model="form.realName" allowClear>
          <a-select-option v-for="(item, index) in personList" :key="index" @click.native="nameChange(item)">{{
    item.realName
}}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="身份证号" prop="identification">
        <a-input v-model="form.identification" :disabled="isDisabled1" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="性别" prop="gender">
        <a-select v-model="form.gender" :disabled="isDisabled1" placeholder="请选择">
          <a-select-option value="1">男</a-select-option>
          <a-select-option value="2">女</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="户籍地详址" prop="householdAddress">
        <a-input v-model="form.householdAddress" :disabled="isDisabled1" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="现居住地详址" prop="address">
        <a-input v-model="form.address" :disabled="isDisabled1" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="就业单位" prop="employUnit">
        <a-input v-model="form.employUnit" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="就业时间" prop="employTime">
        <a-date-picker v-model="form.employTime" style="width:100%;" valueFormat="YYYY-MM-DD" placeholder="请选择" />
      </a-form-model-item>
      <a-form-model-item label="就业地点" prop="employPlace">
        <a-input v-model="form.employPlace" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="就业类型" prop="employType">
        <a-select v-model="form.employType" placeholder="请选择" style="width:100%;">
          <a-select-option v-for="(item, key) in employTypeArr" :key="key" :value="item.value">{{ item.title }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="岗位">
        <a-input v-model="form.post" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="工资待遇" prop="salaryTreatment">
        <a-input v-model="form.salaryTreatment" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="联系电话">
        <a-input v-model="form.contactNumber" placeholder="请输入" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as employmentApi from '@/api/sjsk/help/employment.js'
import * as personServiceApi from '@/api/sjsk/personService.js'
import { getDictItemsFromCache } from '@/api/api'
export default {
  name: 'AddModal',
  data() {
    return {
      visible: false,
      form: {},
      personList: [],
      isDisabled: true,
      isDisabled1: false,
      loading: false,
      rules: {
        name: [{ required: true, message: '请选择' }],
        realName: [{ required: true, message: '请选择' }],
        identification: [{ required: true, message: '请输入' }],
        gender: [{ required: true, message: '请选择' }],
        householdAddress: [{ required: true, message: '请输入' }],
        address: [{ required: true, message: '请输入' }],
        employUnit: [{ required: true, message: '请选择' }],
        employTime: [{ required: true, message: '请选择' }],
        employPlace: [{ required: true, message: '请输入' }],
        employType: [{ required: true, message: '请选择' }],
        salaryTreatment: [{ required: true, message: '请输入' }],
      },
      employTypeArr: getDictItemsFromCache('employType'),
    }
  },
  async created() {
  },
  methods: {
    show(type, record, departmentId, isEdit) {
      this.visible = true
      this.form = {}
      //直接添加和就业安置都要传id
      if (type == 1) { //专干,直接添加就业安置
        this.isDisabled = false
        this.isDisabled1 = true
        this.form.type = 1 //1:编辑和直接添加 2:就业安置
        this.getSJSK(departmentId) //获取社戒社康人员
      } else { //职能部门，对列表人员就业安置
        this.isDisabled = true
        this.isDisabled1 = true
        console.log("isEdit")
        console.log(isEdit)
        if (isEdit) { //编辑
          this.form.type = 1 //编辑
          this.form = record //更新的时候是记录数据回填
        } else { //对列表就业安置
          this.form.type = 2 //对列表人员就业安置
          this.form.id = record.id //新增的时候是记录人员id
          this.form.personId = record.personId
          this.form.name = record.name
          this.form.identification = record.identification
          this.form.gender = record.gender
          this.form.householdAddress = record.householdAddress
          this.form.address = record.address
        }

      }

    },
    onTimeChange() {

    },
    getSJSK(departmentId) {
      let pageFilter = {
        pageNo: 1,
        pageSize: 1000,
        departmentId: departmentId
      }
      personServiceApi
        .getNotEmploymentPersonList(pageFilter)
        .then(res => {
          if (res.success) {
            this.personList = res.result.records
          }
        })
        .catch(e => {
          this.$message.warn('请求失败！')
        })
    },
    nameChange(e) {
      console.log(e)
      this.form.personId = e.id //设置personId
      this.form.name = e.realName
      this.form.identification = e.identityCard
      this.form.gender = e.sex.toString()
      this.form.householdAddress = e.householdAddress
      this.form.address = e.currentAddress
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          employmentApi.addEmployPlacement(this.form).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-form-item {
  margin-bottom: 10px;
}
</style>
